package transaction;

/**
 * @author chaird
 * @create 2022-01-08 20:24
 */
import org.apache.rocketmq.client.producer.LocalTransactionExecuter;
import org.apache.rocketmq.client.producer.LocalTransactionState;
import org.apache.rocketmq.common.message.Message;

import java.util.Date;

public class TransactionExecuterimpl implements LocalTransactionExecuter {
  @Override
  public LocalTransactionState executeLocalTransactionBranch(
      final Message message, final Object o) {
    try {
      String msg = new String(message.getBody(), "UTF-8");
      System.out.println("TransactionExecuterimpl-------" + msg);
      // db.save()
      // db.save()
      // db.save()
      int i = 1/0;
      System.out.println(new Date() + "===> 本地事务执行成功，发送确认消息");
    } catch (Exception e) {
      System.out.println(new Date() + "===> 本地事务执行失败,回滚！！！");
      // connect.rollback()
      return LocalTransactionState.ROLLBACK_MESSAGE;
    }
    return LocalTransactionState.COMMIT_MESSAGE;
  }
}
